﻿using Furion.DependencyInjection;
using Microsoft.Extensions.Caching.Memory;
using MyProject.Entity.SystemManage;
using MyProject.Enum;
using MyProject.IService.SystemManage;
using MyProject.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyProject.Cache
{
    public class MenuCache : ITransient
    {
        private readonly string MenuCacheKey = CacheKeys.MenuCache.ParseToString();

        private readonly IMemoryCache _memoryCache;
        private readonly IMenuService _menuService;

        public MenuCache(IMemoryCache memoryCache, IMenuService menuService)
        {
            _memoryCache = memoryCache;
            _menuService = menuService;
        }


        public async Task<List<MenuEntity>> GetMenuCacheList()
        {
            var cacheList = _memoryCache.Get<List<MenuEntity>>(MenuCacheKey);
            if (cacheList == null || cacheList.Count() == 0)
            {
                cacheList = await _menuService.GetList(null);
                _memoryCache.Set(MenuCacheKey, cacheList);
            }
                
            return cacheList;
        }

    }
}